<?php
declare (strict_types = 1);

namespace app\validate;

use think\Validate;

class Goods extends Validate
{
    /**
     * 定义验证规则
     * 格式：'字段名' =>  ['规则1','规则2'...]
     *
     * @var array
     */
    protected $rule = [
        'goods_name' => 'require|max:25|unique:goods',
        'price'      => 'require|float|egt:vip',
        'vip'        => 'require|float|elt:price',
        'spec_qty'   => 'require|number',
        'default_image' => 'image|filesize:409600|fileExt:jpg,png|fileMime:image/jpeg,image/png',
        'image_url'   => 'image|filesize:409600|fileExt:jpg,png|fileMime:image/jpeg,image/png',
        'cate_id'      =>'require',
        'brand'         =>'require',
        'close_reason'  =>'chsAlphaNum|max:255',
        'description'   =>'require',
    ];

    protected $field = [
        'goods_name'  => '商品名称',
        'price'       => '市场价格',
        'vip'         => '会员价',
        'spec_qty'    => '库存数量',
        'default_image'  => '商品默认图片',
        'image_url'    => '商品轮播图',
        'cate_id'       =>'分类标题',
        'brand'         =>'品牌',
        'close_reason'  =>'下架原因',
        'description'   =>'描述',
    ];

    /**
     * 定义错误信息
     * 格式：'字段名.规则名' =>  '错误信息'
     *
     * @var array
     */
    protected $message = [
        'goods_name.require' => '用户名不能为空',
        'goods_name.unique' => '用户名已重复',
        'goods_name.max'     => '用户名长度不能超过25个字符',
        'price.require'     =>'市场价格不能为空',
        'price.float'     =>'市场价格为小数或者整数',
        'default_image.filesize'  => '商品默认图片大小超过限制',
        'default_image.image'  => '商品默认图片必须是图片格式',
        'default_image.fileMime'  => '商品默认图片类型不正确',
        'default_image.fileExt'  => '商品默认图片格式不正确',
        'image_url.filesize'    => '商品轮播图片大小超过限制',
        'image_url.image'    => '商品轮播图片必须是图片格式',
        'cate_id.require'     =>'分类不能为空',
        'brand.require'     =>'品牌不能为空',
    ];


    public function sceneUpload(){
        return $this->only(['default_image','image_url']);
    }

    public function sceneAdd(){
        return $this->only(['goods_name','price','vip','spec_qty','cate_id','brand','close_reason','description']);
    }
    public function sceneEdit(){
        return $this->only(['goods_name','price','vip','spec_qty','cate_id','brand','close_reason','description','default_image','image_url'])
        ->remove('goods_name','unique');
    }
}
